﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Net.Mail;
using System.Net;
using System.Text.RegularExpressions;

namespace PHTShop
{
    public partial class WebForm6 : basePageUser
    {
        private DataContextDataContext dc = new DataContextDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            switch (Session["languages"].ToString().ToLower())
            {
                case "france":
                    Page.Title = "Inscrivez-vous";
                    break;
                case "germany":
                    Page.Title = "Registrieren";
                    break;
                default:
                    Page.Title = "Register";
                    break;
            }
        }

        protected void Page_PreInit(object sender, EventArgs e)
        {
            Setting th = this.dc.Settings.Single(t => t.type.Equals("theme") && Convert.ToInt32(t.value) == 1);
            Page.Theme = th.name.ToString();
        }

        protected void register_click(object sender, EventArgs e)
        {
            this.LabelUsername.Text = "";
            this.LabelPassword.Text = "";
            this.LabelrePass.Text = "";
            this.LabelfullName.Text = "";
            this.Labelemail.Text = "";
            if (this.check(this.username.Value) == true)
            {
                this.LabelUsername.Text = "not null";
            }
            else if (this.check(this.password.Value) == true)
            {
                this.LabelPassword.Text = "not null";
            }
            else if (this.check(this.rePass.Value) == true || this.rePass.Value.Equals(this.password.Value) == false)
            {
                this.LabelrePass.Text = "not null and equal Password";
            }
            else if (this.check(this.fullname.Value) == true)
            {
                this.LabelfullName.Text = "not null";
            }
            else if (this.check(this.email.Value) == true || this.isValidEmail(this.email.Value) == false)
            {
                this.Labelemail.Text = "not null and correct format";
            }
            else
            {
                if (checkAccount() == false)
                {
                    Account ac = new Account();
                    ac.username = this.username.Value;
                    ac.password = FormsAuthentication.HashPasswordForStoringInConfigFile(this.password.Value, "sha1");
                    ac.fullname = this.fullname.Value;
                    ac.address = this.address.Value;
                    ac.phone = this.phone.Value;
                    ac.email = this.email.Value;
                    ac.active = true;
                    ac.roleId = 2;

                    this.dc.Accounts.InsertOnSubmit(ac);
                    this.dc.SubmitChanges();

                    this.sendMail();
                    Response.Redirect("Myaccount.aspx");

                }
                else
                {
                    this.LabelUsername.Text = "Username already";
                }
            }
        }

        private void sendMail()
        {
            Setting email = this.dc.Settings.Single(t => t.type.Equals("Email") && t.name.Equals("Email"));
            Setting password = this.dc.Settings.Single(t => t.type.Equals("Email") && t.name.Equals("Password"));
            try
            {
                MailMessage msg = new MailMessage();
                msg.From = new MailAddress(email.value);
                msg.To.Add(this.email.Value);
                msg.Subject = "Thanks " + this.fullname.Value;
                msg.Body = "Username : " + this.username.Value + "\nPassword : " + this.password.Value + "\nFullname: " + this.fullname.Value + "\nPhone : " + this.phone.Value + "\nEmail : " + this.email.Value + "\nAddress : " + this.address.Value ;

                SmtpClient sc = new SmtpClient("smtp.gmail.com");
                sc.Port = 25;
                sc.Credentials = new NetworkCredential(email.value, password.value);
                sc.EnableSsl = true;
                sc.Send(msg);
            }
            catch
            {

            }
        }

        private bool checkAccount()
        {
            try
            {
                Account ac = this.dc.Accounts.Single(t => t.username.Equals(this.username.Value));
                return true;
            }
            catch
            {      
                return false;
            }
        }

        private bool check(string str)
        {
            return String.IsNullOrEmpty(str.Trim());
        }

        private bool isValidEmail(string inputEmail)
        {
            string strRegex = @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";

            Regex re = new Regex(strRegex);

            if (re.IsMatch(inputEmail))
                return (true);
            else
                return (false);
        }  

    }
}